<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item to="/home">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入名称" v-model="queryInfo.query" @keydown.enter.native="getOrderList" @clear="getOrderList" clearable>
                        <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <el-table :data="orderList" row-key="order_id" border stripe>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="订单编号" prop="order_number"></el-table-column>
                <el-table-column label="价格" prop="order_price" width="80px"></el-table-column>
                <el-table-column label="支付状态" width="80px">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.pay_status=='1'">已付款</el-tag>
                        <el-tag type="danger" v-else>未付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="已发货" prop="is_send" width="80px"></el-table-column>
                <el-table-column label="操作" width="120px">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="changeAddress(scope.row)"></el-button>
                        <el-button size="mini" type="success" icon="el-icon-position" @click="showKuaidi(scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 10, 20, 50]"
                :page-size="queryInfo.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
        <el-dialog title="修改收货地址" :visible.sync="changeAddressDialogVisible" width="50%">
            <el-form>
                <el-form-item label="收货地址">
                    <el-cascader :options="citydata"
                        :props="{ expandTrigger: 'hover' }">
                    </el-cascader>
                </el-form-item>
                <el-form-item label="详细地址">
                    <el-input type="textarea"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="changeAddressDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="changeAddressDialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="查看物流信息" :visible.sync="kuaidiDialogVisible" width="80%">
            <el-timeline>
                <el-timeline-item
                v-for="(activity, index) in kuaidiList"
                :key="index"
                :color="index==0?'green':'lightgray'"
                :timestamp="activity.time">
                {{activity.context}}
                </el-timeline-item>
            </el-timeline>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="kuaidiDialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import citydata from './citydata'
export default {
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            total: 0,
            orderList: [],
            changeAddressDialogVisible: false,
            kuaidiDialogVisible: false,
            citydata,
            kuaidiList: [], 
        }
    },
    created() {
        console.log(citydata);
        this.getOrderList();
    },
    methods: {
        async getOrderList() {
            const {data:res} = await this.$http.get('orders',{params: this.queryInfo});
            if(res.meta.status!=200) {
                return this.$message.error('获取订单列表失败：'+res.meta.message);
            }
            console.log('orders',res.data.goods);
            this.orderList=res.data.goods;
            this.pagenum=res.data.pagenum;
            this.total=res.data.total;
        },
        handleSizeChange(newSize) {
            this.queryInfo.pagesize=newSize;
            this.getOrderList();
        },
        handleCurrentChange(pagenum) {
            console.log('pagenum',pagenum)
            this.queryInfo.pagenum=pagenum;
            this.getOrderList();
        },
        changeAddress(order) {
            this.changeAddressDialogVisible=true;
        },
        async showKuaidi(order) {
            const {data:res} = await this.$http.get('kuaidi/'+order.order_id);
            if(res.meta.status!=200) {
                return this.$message.error('获取物流信息失败:'+res.meta.msg);
            }

            this.kuaidiList=res.data;
            console.log('kuaidi',res.data);
            this.kuaidiDialogVisible=true;
        }
    }
}
</script>

<style lang="less" scoped>

</style>